<?php

namespace app\model;

use think\facade\Config;
use think\Model;

class SysRole extends Model
{
    public function addOrUpdate(): array
    {
        $param = request()->post();
        $this->exists(!empty($param['id']))->save($param);
        return $this->toArray();
    }

    public function lst(): array
    {
        $param = request()->param();
        return pageData($this->order(['create_time' => 'desc'])->paginate(['list_rows' => $param['size'] ?? 30, 'page' => $param['page'] ?? 1]));
    }

    public function detail()
    {
        $id = request()->param('id');
        $data = $this->find($id);
        if (!empty($data)) {
            $data = $data->toArray();
            $config = Config::get("menu.protected");
            //获取相同项
            $lstMenu = array_intersect($config, $data['menu_lst']);
            $data['menu'] = array_values($lstMenu);
        }
        return $data;
    }

    public function search(): array
    {
        return $this->where('state', 1)->column("id,name");
    }
}
